package edu.dqsy.mzq_translate;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import edu.dqsy.mzq_translate.helper.ContactOpenHelper;

import static edu.dqsy.mzq_translate.denglu.user;

public class Danciben extends AppCompatActivity {
    public String username;
    private ListView lv;
    private ArrayList<dancilist> dancilists;
    final ContactOpenHelper helper = new ContactOpenHelper(Danciben.this,"userinfo.db",null,1);

    @Override
    protected void onRestart() {
        super.onRestart();
        this.loadtable();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_danciben);
        username = user;
        System.out.println(username);
        this.loadtable();


    }

    public void loadtable(){
        helper.getWritableDatabase();
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT * FROM "+username,null);
        dancilists = new ArrayList<>();
        if(c!=null){
            while(c.moveToNext()){
                String name = c.getString(1);
                String info = c.getString(2);
                System.out.println(name+":"+info);
                dancilist st = new dancilist(name,info);
                System.out.println(st.getName()+"st属性"+st.getInfo());
                if (st!=null){
                    dancilists.add(st);
                }

            }
        }

        lv = findViewById(R.id.dancibens);

        lv.setAdapter(new BaseAdapter() {
            /*
             * 为ListView设置一个适配器
             * getCount()返回数据个数
             * getView()为每一行设置一个条目
             * */
            @Override
            public int getCount() {
                return dancilists.size();
            }

            @Override
            public Object getItem(int position) {
                // return studentlist.get(position);
                return null;
            }

            @Override
            public long getItemId(int position) {

                // return position;
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {


                View view ;
                /**对ListView的优化，convertView为空时，创建一个新视图；
                 * convertView不为空时，代表它是滚出,
                 * 放入Recycler中的视图,若需要用到其他layout，
                 * 则用inflate(),同一视图，用fiindViewBy()
                 * **/
                if(convertView == null )
                {
                    LayoutInflater inflater = Danciben.this.getLayoutInflater();
                    view = inflater.inflate(R.layout.item,null);
                    //view = View.inflate(getBaseContext(),R.layout.item,null);
                }
                else
                {
                    view = convertView;
                }
                //从studentlist中取出一行数据，position相当于数组下标,可以实现逐行取数据
                dancilist st = dancilists.get(position);
//                TextView name = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null).findViewById(R.id.stu_name);
                //TextView xuehao = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null).findViewById(R.id.stu_number);
                //TextView Class = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null).findViewById(R.id.stu_age);
                Button ttt = view.findViewById(R.id.intodanci);
                TextView name = (TextView)view.findViewById(R.id.dcinfo);
                TextView info  = (TextView)view.findViewById(R.id.dcname);
                Button del = view.findViewById(R.id.deldanci);
                ttt.setOnClickListener(view1 -> {
                    Intent ii = new Intent(Danciben.this, Danciliebiao.class);
                    ii.putExtra("danciname",username+"_"+name.getText().toString());
                    startActivity(ii);
                });
                del.setOnClickListener(view1 -> {
                    SQLiteDatabase db = helper.getWritableDatabase();
                    db.execSQL("DELETE FROM  "+username+" WHERE name =? ",new String[]{name.getText().toString()});
                    db.execSQL("drop table "+username+"_"+name.getText().toString());
                    loadtable();
                    db.close();
                });

                info.setText(st.getInfo());
                name.setText(st.getName());

                return view;
            }
        });
    }


    public void addtable(View v){
        Intent ii = new Intent(Danciben.this, CreateDancitable.class);
        startActivity(ii);

    }


    public void exit(View v){
        Intent i = new Intent(Danciben.this, denglu.class);
        startActivity(i);
    }
    public void fanyi(View v){
        Intent i = new Intent(Danciben.this, MainActivity.class);
        startActivity(i);
    }
}
class dancilist{
    String name;
    String info;

    public dancilist(String name, String info) {
        this.name = name;
        this.info = info;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }


}